package com.hrzyit.whistle.cases.ui.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import com.hrzyit.base.ui.activity.BaseActivity
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.ui.fragment.IndexDeptTransferFragment
import com.hrzyit.whistle.cases.ui.fragment.SelectDeptAndUserForApproveFragment
import java.util.*

/**
 * 选择单位和人员 Activity, 不同选择单位和人员 Fragment 界面的壳 *!*
 */
class SelectDeptAndUserActivity : BaseActivity() {

    // Fragment 栈
    private var mStack: Stack<Fragment> = Stack()

    /**
     * 批示单位选择界面 Fragment
     */
    private val selectDeptAndUserForApproveFragment: SelectDeptAndUserForApproveFragment by lazy {
        SelectDeptAndUserForApproveFragment()
    }

    /**
     * 批转单位选择界面 Fragment
     */
    private val indexDeptTransferFragment: IndexDeptTransferFragment by lazy {
        IndexDeptTransferFragment()
    }
    /**
     * 批转人员选择界面 Fragment
     */
//    private val indexUserTransferFragment: IndexUserTransferFragment by lazy {
//        IndexUserTransferFragment()
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_dept_and_user)

        // 获取显示的 Fragment 标签
        val tag = intent.getStringExtra("tag")

        initFragment()

        changeFragmentByTag(tag)
    }

    /**
     * 初始化Fragment栈管理
     */
    private fun initFragment() {

        val manager = supportFragmentManager.beginTransaction()

        manager.add(
            R.id.mSelectDeptAndUserContainer,
            selectDeptAndUserForApproveFragment,
            CaseConstant.TAG_FRAGMENT_FOR_APPROVE
        )
        manager.add(
            R.id.mSelectDeptAndUserContainer,
            indexDeptTransferFragment,
            CaseConstant.TAG_FRAGMENT_DEPT_INDEX_FOR_TRANSFER
        )

//        manager.add(
//            R.id.mSelectDeptAndUserContainer,
//            indexUserTransferFragment,
//            CaseConstant.TAG_FRAGMENT_USER_INDEX_FOR_TRANSFER
//        )
        manager.commit()

        mStack.add(selectDeptAndUserForApproveFragment)
        mStack.add(indexDeptTransferFragment)
//        mStack.add(indexUserTransferFragment)
    }

    /**
     * 通过 Tag 切换 Fragment
     */
    private fun changeFragmentByTag(tag: String) {
        val manager = supportFragmentManager.beginTransaction()
        mStack.forEach{
            when (it.tag) {
                tag -> {
                    manager.show(it)
                }
                else -> {
                    manager.hide(it)
                }
            }
        }
        manager.commit()
    }
}
